﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Text.RegularExpressions;
using University.Workshop.DataAccess;
using System.Globalization;

namespace University.Workshop.Business
{
    public static class BidManager
    {

        public static Auction GetAuctionByproduct(long productId)
        {
                try
                {
                    return AuctionDataAccess.GetAuctionByproductId(productId);
                }
                catch(Exception)
                {
                    throw new InvalidOperationException("Error getting auction");
                }
        }

        public static bool Bid(string lastBidPrice, string newBidPrice, long bidderUserId,String productId)
        {
            if((String.IsNullOrEmpty(newBidPrice)))
            {
                throw new ValidationException("All fields are required");
            }
            string numbers = @"^[0-9]+$";
            if (!Regex.IsMatch(newBidPrice, numbers))
            {
                throw new ValidationException("Wrong amount");
            }
            Auction auction = new Auction();
            auction.LastBidPrice = Convert.ToInt64(lastBidPrice,CultureInfo.CurrentCulture);
            auction.ProductId = Convert.ToInt64(productId,CultureInfo.CurrentCulture);
            float nBidPrice = Convert.ToInt64(newBidPrice,CultureInfo.CurrentCulture);
            bool exit=true;
            try
            {
                if (Regex.Equals(lastBidPrice, newBidPrice))
                {
                        throw new BusinessException("Your Bid must be higher");
                }
                else
                {

                    if (nBidPrice > auction.LastBidPrice)
                    {
                        bool answer = AuctionDataAccess.UpdateBid(false,nBidPrice,auction.ProductId,DateTime.Now,bidderUserId);
                        if (answer)
                            exit = true;
                        else
                        {
                            exit = false;
                            throw new BusinessException("Connection error");
                        }

                    }
                    else
                    {
                        throw new BusinessException("Your Bid must be higher");
                    }
                }
            }
            catch (Exception)
            {
                throw new BusinessException("Your Bid must be higher");
            }

            return exit;
        }

        public static AuctionState EndAuction(DateTime date, long productId)
        {
            try
            {
                if (Regex.Equals(DateTime.Now, date))//si la fecha actual es igual a la fecha limite
                {
                    throw new BusinessException("Auction is closed");
                }
                if (DateTime.Now >= date)
                {
                    AuctionDataAccess.UpdateBid(true, 0, productId, DateTime.Now, 0);
                    //TODO. Sent email to the winner and to the seller
                    return AuctionState.Finished;
                }
                else
                    return AuctionState.InProcess;
            }
            catch (Exception)
            {
                throw new BusinessException("Auction is closed");
            }
        }
    }

}
